import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer';
import tailwindcss from '@tailwindcss/postcss';
import fs from 'fs';
import path from 'path';

import packageJson from './package.json';

// 自定义插件，用于复制data.md文件到dist目录
const copyDataMd = () => {
  return {
    name: 'copy-data-md',
    writeBundle() {
      // 确保dist目录存在
      if (!fs.existsSync('dist')) {
        fs.mkdirSync('dist');
      }
      
      // 复制data.md文件到dist目录
      fs.copyFileSync(
        path.resolve('data.md'),
        path.resolve('dist', 'data.md')
      );
      console.log('Successfully copied data.md to dist directory');
    }
  };
};

export default {
  input: 'src/index.ts',
  output: [
    {
      file: packageJson.main,
      format: 'cjs',
      sourcemap: true
    },
    {
      file: packageJson.module,
      format: 'esm',
      sourcemap: true
    }
  ],
  plugins: [
    peerDepsExternal(),
    resolve(),
    commonjs(),
    typescript({
      useTsconfigDeclarationDir: true
    }),
    postcss({
      plugins: [
        tailwindcss('./tailwind.config.js'),
        autoprefixer(),
      ],
      extract: 'markdown-renderer.css',
      minimize: true,
      modules: false,
      autoModules: false,
    }),
    terser(),
    copyDataMd()  // 添加自定义复制插件
  ],
  external: Object.keys(packageJson.dependencies || {})
};